/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PesquisaOrcamentos.java
 *
 * Created on 13/09/2011, 09:28:53
 */

package sisgraf.view;
import com.itextpdf.text.DocumentException;
import java.io.IOException;
import java.net.MalformedURLException;
import sisgraf.impressao.Imprimir;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import sisgraf.controler.OrcamentoControler;
import sisgraf.controler.OrcamentoTabela;
import sisgraf.dao.VendedorDaoImp;
import sisgraf.impressao.ImprimePdf;
import sisgraf.model.ItemOrcamento;
import sisgraf.model.Orcamento;
import sisgraf.model.Vendedor;


/**
 *
 * @author Jordan de Carvalho Guerra
 */
public class PesquisaOrcamentos extends javax.swing.JDialog {
    private OrcamentoControler modeloControler =new OrcamentoControler();
    private OrcamentoTabela modelo;
    private Vendedor vend;
    private String condicao;

    /** Creates new form PesquisaOrcamentos */
    public PesquisaOrcamentos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        formComponentResized(null);
        setLocationRelativeTo(null);
        jBtIncluir.setVisible(Menu.estadoPerfil.getPermissao("orc", "I"));
        jBtAlterar.setVisible(Menu.estadoPerfil.getPermissao("orc", "A"));
        jBtExcluir.setVisible(Menu.estadoPerfil.getPermissao("orc", "E"));
        jBtConsultar.setVisible(Menu.estadoPerfil.getPermissao("orc", "C"));
        jBtImprimir.setVisible(Menu.estadoPerfil.getPermissao("orc", "I"));
        jTbVisualizacao.setModel(modeloControler.getListarOrcamento(null));
        List<Vendedor> lista = null;
        vend = null;
        lista = new VendedorDaoImp().list("v.usuario.id = '"+Menu.estadoUsuario.getId()+"'");
        if (lista.isEmpty()&&!Menu.estadoPerfil.getPermissao("vnd", "P")){
           javax.swing.JOptionPane.showMessageDialog(null, "Usuário não é vendedor e nem possui acesso a vendedores" , "Controle de Acesso", javax.swing.JOptionPane.INFORMATION_MESSAGE);
           this.dispose();
        }
        if (!Menu.estadoPerfil.getPermissao("vnd", "P")){
           vend=lista.get(0);
           jTxFdNomVen.setText(vend.getNom_ven());
           jTxFdNomVen.setEnabled(false);

        }
        modeloControler.ajustarTabela(jTbVisualizacao);
        jBtAlterar.setEnabled(false);
        jBtExcluir.setEnabled(false);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGrpTipoPeriodo = new javax.swing.ButtonGroup();
        jPnlSuperior = new javax.swing.JPanel();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
        jPnlCentral = new javax.swing.JPanel();
        jPnlConteudo = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTxFdNomCli = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTxFdNomVen = new javax.swing.JTextField();
        javax.swing.JLabel jLabel20 = new javax.swing.JLabel();
        jTxFdDatIni = new javax.swing.JFormattedTextField();
        javax.swing.JLabel jLabel21 = new javax.swing.JLabel();
        jTxFdDatFim = new javax.swing.JFormattedTextField();
        jRdBtSolicitacao = new javax.swing.JRadioButton();
        jRdBtEmissao = new javax.swing.JRadioButton();
        jCkBxEncerrados = new javax.swing.JCheckBox();
        jBtPesquisar = new javax.swing.JButton();
        jBtFinalizar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTbVisualizacao = new javax.swing.JTable();
        jTxFdNumOrc = new javax.swing.JFormattedTextField();
        jPnlControle = new javax.swing.JPanel();
        jBtExcluir = new javax.swing.JButton();
        jBtAlterar = new javax.swing.JButton();
        jBtIncluir = new javax.swing.JButton();
        jBtImprimir = new javax.swing.JButton();
        jBtConsultar = new javax.swing.JButton();
        jPnlInferior = new javax.swing.JPanel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jPnlSuperior.setBorder(new javax.swing.border.MatteBorder(null));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("SISTEMA DE ORÇAMENTOS PARA SERVIÇOS GRAFICOS");
        jLabel1.setMinimumSize(new java.awt.Dimension(0, 14));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Pesquisa Orçamentos");
        jLabel2.setMinimumSize(new java.awt.Dimension(0, 14));

        javax.swing.GroupLayout jPnlSuperiorLayout = new javax.swing.GroupLayout(jPnlSuperior);
        jPnlSuperior.setLayout(jPnlSuperiorLayout);
        jPnlSuperiorLayout.setHorizontalGroup(
            jPnlSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 779, Short.MAX_VALUE)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 779, Short.MAX_VALUE)
        );
        jPnlSuperiorLayout.setVerticalGroup(
            jPnlSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlSuperiorLayout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPnlCentral.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPnlCentral.setMinimumSize(new java.awt.Dimension(100, 0));

        jPnlConteudo.setBorder(javax.swing.BorderFactory.createLineBorder(null));

        jLabel5.setText("Número:");

        jLabel6.setText("Cliente:");

        jLabel7.setText("Vendedor:");

        jLabel20.setText("Período de ");

        jTxFdDatIni.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));

        jLabel21.setText("à");

        jTxFdDatFim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));

        btnGrpTipoPeriodo.add(jRdBtSolicitacao);
        jRdBtSolicitacao.setText("Solicitação");

        btnGrpTipoPeriodo.add(jRdBtEmissao);
        jRdBtEmissao.setText("Emissão");

        jCkBxEncerrados.setText("Encerrados");

        jBtPesquisar.setText("Pesquisar");
        jBtPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtPesquisarActionPerformed(evt);
            }
        });

        jBtFinalizar.setText("Finalizar");
        jBtFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtFinalizarActionPerformed(evt);
            }
        });

        jTbVisualizacao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTbVisualizacao.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(jTbVisualizacao);

        jTxFdNumOrc.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0"))));

        javax.swing.GroupLayout jPnlConteudoLayout = new javax.swing.GroupLayout(jPnlConteudo);
        jPnlConteudo.setLayout(jPnlConteudoLayout);
        jPnlConteudoLayout.setHorizontalGroup(
            jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlConteudoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 755, Short.MAX_VALUE)
                    .addGroup(jPnlConteudoLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxFdNumOrc, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxFdNomCli, javax.swing.GroupLayout.DEFAULT_SIZE, 606, Short.MAX_VALUE))
                    .addGroup(jPnlConteudoLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxFdNomVen, javax.swing.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE))
                    .addGroup(jPnlConteudoLayout.createSequentialGroup()
                        .addComponent(jLabel20)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxFdDatIni, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel21, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTxFdDatFim, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jRdBtSolicitacao)
                        .addGap(18, 18, 18)
                        .addComponent(jRdBtEmissao, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jCkBxEncerrados)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBtPesquisar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBtFinalizar)))
                .addContainerGap())
        );
        jPnlConteudoLayout.setVerticalGroup(
            jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlConteudoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jTxFdNomCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTxFdNumOrc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTxFdNomVen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxFdDatIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel20)
                    .addComponent(jLabel21)
                    .addComponent(jBtFinalizar)
                    .addComponent(jBtPesquisar)
                    .addComponent(jCkBxEncerrados)
                    .addComponent(jRdBtEmissao)
                    .addComponent(jRdBtSolicitacao)
                    .addComponent(jTxFdDatFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                .addGap(12, 12, 12))
        );

        jPnlControle.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jBtExcluir.setText("Excluir");
        jBtExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtExcluirActionPerformed(evt);
            }
        });

        jBtAlterar.setText("Alterar");
        jBtAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtAlterarActionPerformed(evt);
            }
        });

        jBtIncluir.setText("Incluir");
        jBtIncluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtIncluirActionPerformed(evt);
            }
        });

        jBtImprimir.setText("Imprimir");
        jBtImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtImprimirActionPerformed(evt);
            }
        });

        jBtConsultar.setText("Consultar");
        jBtConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtConsultarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPnlControleLayout = new javax.swing.GroupLayout(jPnlControle);
        jPnlControle.setLayout(jPnlControleLayout);
        jPnlControleLayout.setHorizontalGroup(
            jPnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlControleLayout.createSequentialGroup()
                .addContainerGap(354, Short.MAX_VALUE)
                .addComponent(jBtConsultar)
                .addGap(18, 18, 18)
                .addComponent(jBtIncluir)
                .addGap(18, 18, 18)
                .addComponent(jBtAlterar)
                .addGap(18, 18, 18)
                .addComponent(jBtExcluir)
                .addGap(18, 18, 18)
                .addComponent(jBtImprimir)
                .addContainerGap())
        );
        jPnlControleLayout.setVerticalGroup(
            jPnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlControleLayout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addGroup(jPnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtImprimir)
                    .addComponent(jBtExcluir)
                    .addComponent(jBtAlterar)
                    .addComponent(jBtIncluir)
                    .addComponent(jBtConsultar))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPnlCentralLayout = new javax.swing.GroupLayout(jPnlCentral);
        jPnlCentral.setLayout(jPnlCentralLayout);
        jPnlCentralLayout.setHorizontalGroup(
            jPnlCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPnlConteudo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPnlControle, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPnlCentralLayout.setVerticalGroup(
            jPnlCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlCentralLayout.createSequentialGroup()
                .addComponent(jPnlConteudo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlControle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPnlInferior.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Gráfica Modêlo");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Grupo de Estudo PMA Anhanguera");

        javax.swing.GroupLayout jPnlInferiorLayout = new javax.swing.GroupLayout(jPnlInferior);
        jPnlInferior.setLayout(jPnlInferiorLayout);
        jPnlInferiorLayout.setHorizontalGroup(
            jPnlInferiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlInferiorLayout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 461, Short.MAX_VALUE)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPnlInferiorLayout.setVerticalGroup(
            jPnlInferiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlInferiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel3))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPnlSuperior, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPnlInferior, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPnlCentral, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPnlSuperior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlCentral, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlInferior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        // TODO add your handling code here:
        jPnlControle.setBorder(null);
        jPnlConteudo.setBorder(null);
        int dif = this.getHeight()-jPnlSuperior.getHeight()-jPnlCentral.getHeight()-jPnlInferior.getHeight()-6;
        jPnlCentral.setSize(this.getWidth(),this.getHeight()-jPnlSuperior.getHeight()-jPnlInferior.getHeight()-dif);
        jPnlConteudo.setSize(jPnlCentral.getWidth()-12,jPnlCentral.getHeight()-jPnlControle.getHeight());
        jPnlControle.setSize(jPnlCentral.getWidth()-12, jPnlControle.getHeight());
        jPnlControle.setLocation(2, jPnlConteudo.getHeight());
    }//GEN-LAST:event_formComponentResized

    private void jBtPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtPesquisarActionPerformed
        // TODO add your handling code here:
        condicao = "";
        String conector = "";
        String dataIni = "";
        String dataFim = "";
        String dataPes = "";
        String erroMensagem = "";
        
        String paNumOrc=jTxFdNumOrc.getText().replaceAll("\\.", "");
        if (!paNumOrc.equals("")){
            if (Integer.parseInt(paNumOrc) != 0){
                condicao="o.id = "+paNumOrc;
                conector = " and ";
            }
        }
        if (!jTxFdNomCli.getText().equals("")){
            condicao = condicao + conector + "o.cliente.nom_cli like '"+jTxFdNomCli.getText()+"%'";
            conector = " and ";
        }
        if (!jTxFdNomVen.getText().equals("")){
            condicao = condicao + conector + "o.vendedor.nom_ven like '"+jTxFdNomVen.getText()+"%'";
            conector = " and ";
        }
        if ((btnGrpTipoPeriodo.getSelection()==jRdBtSolicitacao.getModel())||
            (btnGrpTipoPeriodo.getSelection()==jRdBtEmissao.getModel())){
            if (!jTxFdDatIni.getText().equals("")){
                if (!Menu.validaData(jTxFdDatIni.getText())){
                    erroMensagem="Data inicial invalida\n";
                } else {
                    dataIni=jTxFdDatIni.getText();
                }
            }
            if (!jTxFdDatFim.getText().equals("")){
                if (!Menu.validaData(jTxFdDatFim.getText())){
                    erroMensagem="Data final invalida\n";
                } else {
                    dataFim=jTxFdDatFim.getText();
                }
            }
            if (btnGrpTipoPeriodo.getSelection()==jRdBtSolicitacao.getModel()){
                dataPes="o.dataSolicitacao";
            } else {
                dataPes="o.dataEmissao";
            }
            condicao = condicao + conector +dataPes+ " >= '" +Menu.convData(dataIni, 0) + "' and "+dataPes+" <= '" +Menu.convData(dataFim, 0)+"'";
            conector = " and ";
        }
        /*
         *
         */
        if (jCkBxEncerrados.isSelected()){
            condicao = condicao + conector + " o.dataEncerramento is not null ";
            conector = " and ";
        }
        if (!erroMensagem.equals("")){
           javax.swing.JOptionPane.showMessageDialog(null, erroMensagem , "Dados Informados Inválidos", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        } else {
            jTbVisualizacao.setModel(modeloControler.getListarOrcamento(condicao));
            modeloControler.ajustarTabela(jTbVisualizacao);
            if (jTbVisualizacao.getRowCount()>0){
                jBtConsultar.setEnabled(true);
                jBtAlterar.setEnabled(true);
                jBtExcluir.setEnabled(true);
            } else{
                jBtConsultar.setEnabled(false);
                jBtAlterar.setEnabled(false);
                jBtExcluir.setEnabled(false);
            }
        }
    }//GEN-LAST:event_jBtPesquisarActionPerformed

    private void jBtFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtFinalizarActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_jBtFinalizarActionPerformed

    private void jBtExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtExcluirActionPerformed
        // TODO add your handling code here:
        if (jTbVisualizacao.getSelectedRow()>=0) {













            if (javax.swing.JOptionPane.showConfirmDialog(null,"Confirma Exclusão do Orcamento Selecionado ?", "Confirmar Exclusão", javax.swing.JOptionPane.YES_NO_OPTION)==0) {
                int linha = jTbVisualizacao.getSelectedRow();
                modelo = (OrcamentoTabela) jTbVisualizacao.getModel();
                int id = modelo.getOrcamento(linha).getId();
                Orcamento orc=modeloControler.prepararAlterarOrcamento(id);
                if (orc==null){
                    javax.swing.JOptionPane.showMessageDialog(null, "Erro ao preparar Orcamento para exclusão" , "Acesso ao Banco de Dados", javax.swing.JOptionPane.INFORMATION_MESSAGE);
                } else {
                    modelo.setOrcamento(linha, orc);




                    if (!modeloControler.excluirOrcamento(orc)){
                        modeloControler.liberarOrcamento();

                        javax.swing.JOptionPane.showMessageDialog(null, "Erro na exclusão do Orcamento" , "Acesso ao Banco de Dados", javax.swing.JOptionPane.INFORMATION_MESSAGE);
                    }
                    jTbVisualizacao.setModel(modeloControler.getListarOrcamento(condicao));
                    modeloControler.ajustarTabela(jTbVisualizacao);
                }
            }
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "É necessário Selecionar um Orcamento para Exclusão", "Informação de Exclusão", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jBtExcluirActionPerformed

    private void jBtAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtAlterarActionPerformed
        // TODO add your handling code here:
        sisgraf.model.Orcamento orc;
        if (jTbVisualizacao.getSelectedRow()>=0) {
            int linha = jTbVisualizacao.getSelectedRow();
            modelo = (OrcamentoTabela) jTbVisualizacao.getModel();
            int id = modelo.getOrcamento(linha).getId();
            orc=modeloControler.prepararAlterarOrcamento(id);
            if (orc==null){
                javax.swing.JOptionPane.showMessageDialog(null, "Erro ao preparar Orcamento para alteração" , "Acesso ao Banco de Dados", javax.swing.JOptionPane.INFORMATION_MESSAGE);
            } else {
                modelo.setOrcamento(linha, orc);
                Orcamentos orcamentos = new Orcamentos(new javax.swing.JFrame(), true);
                orcamentos.setOrcamento(orc);
                orcamentos.setControler(modeloControler);
                orcamentos.setPesquisaVend(Menu.estadoPerfil.getPermissao("vnd", "P"));
                orcamentos.setOpcao(Menu.OP_PESQ_ALT);
                jTbVisualizacao.setModel(modeloControler.getListarOrcamento(condicao));
                modeloControler.ajustarTabela(jTbVisualizacao);
            }
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "É necessário Selecionar um Orçamento para Alteração", "Informação de Alteração", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jBtAlterarActionPerformed

    private void jBtIncluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtIncluirActionPerformed
        // TODO add your handling code here:
        Orcamentos orcamentos = new Orcamentos(new javax.swing.JFrame(), true);
        Orcamento orcamento = new Orcamento();
        if (!Menu.estadoPerfil.getPermissao("vnd", "P")){
           orcamento.setVendedor(vend);
        }
        orcamentos.setOrcamento(orcamento);
        orcamentos.setControler(modeloControler);
        orcamentos.setPesquisaVend(Menu.estadoPerfil.getPermissao("vnd", "P"));
        orcamentos.setOpcao(Menu.OP_PESQ_INC);
        if (orcamentos.getncluiu()){
            if (condicao.equals("")){
                jTbVisualizacao.setModel(modeloControler.getListarOrcamento(condicao));
                modeloControler.ajustarTabela(jTbVisualizacao);
            } else {
                jTbVisualizacao.setModel(modeloControler.getListarOrcamento("o.id = "+orcamento.getId()));
                modeloControler.ajustarTabela(jTbVisualizacao);
            }
        }
        /*
         * retirar
         *
         */
    }//GEN-LAST:event_jBtIncluirActionPerformed

    private void jBtImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtImprimirActionPerformed
/*
        if(jTbVisualizacao.getRowCount()==0)
         JOptionPane.showMessageDialog(null,"Não há dados para impressão!");
     else
     {
        ArrayList lista = new ArrayList();
        int i=0,j=0;
        for(j=0;j<jTbVisualizacao.getColumnCount();j++)
            lista.add(jTbVisualizacao.getColumnName(j));
        for(i=0;i<jTbVisualizacao.getRowCount();i++)
            for(j=0;j<jTbVisualizacao.getColumnCount();j++)
            lista.add(jTbVisualizacao.getValueAt(i,j));
    Imprimir imprimir = new Imprimir(lista,jTbVisualizacao.getColumnCount());
    Impressao impr = new Impressao(new JFrame(), true);
        impr.setVisible(true);
       }
 *
 */
        ArrayList listaOrcamento = new ArrayList();
         ArrayList listaItemOrcamento = new ArrayList();
        if (jTbVisualizacao.getSelectedRow()>=0) {
            int linha = jTbVisualizacao.getSelectedRow();
            modelo = (OrcamentoTabela) jTbVisualizacao.getModel();
// ******************   Campos para impressão   ********************************
           listaOrcamento.add( modelo.getOrcamento(linha).getId().toString()); // número do orçamento
           listaOrcamento.add(modelo.getOrcamento(linha).getCliente().getNom_cli());
           listaOrcamento.add(modelo.getOrcamento(linha).getCliente().getCpf_cnpj());
           listaOrcamento.add(modelo.getOrcamento(linha).getCliente().getEmail());
            // demais campos do cliente
           listaOrcamento.add( modelo.getOrcamento(linha).getVendedor().getNom_ven());
            // demais campos do vendedor
            listaOrcamento.add(modelo.getOrcamento(linha).getDataEmissaoForm());
            // demais campos do orçamento as datas terminadas em Form retornam
            // com a data no formato dd/mm/aaaa
            modelo.getOrcamento(linha).getItemOrcamento();

            // retorna um set dos itens de orçamento utilizar o iterator para percorrer o set
            ItemOrcamento pesq;
            int w=0;
            Iterator it = modelo.getOrcamento(linha).getItemOrcamento().iterator();
            while (it.hasNext())
            {
               pesq= (ItemOrcamento) it.next();
               listaItemOrcamento.add(pesq.getDescricao());
               listaItemOrcamento.add(pesq.getPrazoEntrega());
               listaItemOrcamento.add(pesq.getQuantidade());
               listaItemOrcamento.add(pesq.getValor());
               // incluir demais campos para cada item de orçamento
            }
            ImprimePdf imp = new ImprimePdf();
            try {
                imp.imprimirOrcamento(listaOrcamento, listaItemOrcamento);
                // ******************* Fim dos campos para impressão    ************************
            } catch (DocumentException ex) {
                Logger.getLogger(PesquisaOrcamentos.class.getName()).log(Level.SEVERE, null, ex);
            }catch (MalformedURLException ex) {
                Logger.getLogger(PesquisaOrcamentos.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(PesquisaOrcamentos.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "É necessário Selecionar um Orçamento para Impressão", "Informação de Alteração", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }

    }//GEN-LAST:event_jBtImprimirActionPerformed

    private void jBtConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtConsultarActionPerformed
        // TODO add your handling code here:
        sisgraf.model.Orcamento orc;
        if (jTbVisualizacao.getSelectedRow()>=0) {
            int linha = jTbVisualizacao.getSelectedRow();
            modelo = (OrcamentoTabela) jTbVisualizacao.getModel();
//            int id = modelo.getOrcamento(linha).getId();
//            orc=modeloControler.prepararAlterarOrcamento(id);
            orc=modelo.getOrcamento(linha);
            if (orc==null){
                javax.swing.JOptionPane.showMessageDialog(null, "Erro ao preparar Orcamento para consulta" , "Acesso ao Banco de Dados", javax.swing.JOptionPane.INFORMATION_MESSAGE);
            } else {
                modelo.setOrcamento(linha, orc);
                Orcamentos orcamentos = new Orcamentos(new javax.swing.JFrame(), true);
                orcamentos.setOrcamento(orc);
                orcamentos.setControler(modeloControler);
                orcamentos.setPesquisaVend(Menu.estadoPerfil.getPermissao("vnd", "P"));
                orcamentos.setOpcao(Menu.OP_PESQ_CON);
//                jTbVisualizacao.setModel(modeloControler.getListarOrcamento(condicao));
//                modeloControler.ajustarTabela(jTbVisualizacao);
            }
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "É necessário Selecionar um Orçamento para Consulta", "Informação de Alteração", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }

    }//GEN-LAST:event_jBtConsultarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGrpTipoPeriodo;
    private javax.swing.JButton jBtAlterar;
    private javax.swing.JButton jBtConsultar;
    private javax.swing.JButton jBtExcluir;
    private javax.swing.JButton jBtFinalizar;
    private javax.swing.JButton jBtImprimir;
    private javax.swing.JButton jBtIncluir;
    private javax.swing.JButton jBtPesquisar;
    private javax.swing.JCheckBox jCkBxEncerrados;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPnlCentral;
    private javax.swing.JPanel jPnlConteudo;
    private javax.swing.JPanel jPnlControle;
    private javax.swing.JPanel jPnlInferior;
    private javax.swing.JPanel jPnlSuperior;
    private javax.swing.JRadioButton jRdBtEmissao;
    private javax.swing.JRadioButton jRdBtSolicitacao;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTbVisualizacao;
    private javax.swing.JFormattedTextField jTxFdDatFim;
    private javax.swing.JFormattedTextField jTxFdDatIni;
    private javax.swing.JTextField jTxFdNomCli;
    private javax.swing.JTextField jTxFdNomVen;
    private javax.swing.JFormattedTextField jTxFdNumOrc;
    // End of variables declaration//GEN-END:variables

}
